class code2 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        //遍历字符串
        for(int i=0;i<s.length();i++) {
            char ch = s.charAt(i);
            if(ch=='{' || ch=='[' || ch == '(') {
                //左括号入栈
                stack.push(ch);
            } else {
                //右括号
                if(stack.isEmpty()) {
                    //栈为空
                    return false;
                }
                //栈不为空，右括号判断匹配
                char ch2 = stack.peek();
                if(ch2=='{'&&ch=='}'||ch2=='['&&ch==']'||ch2=='('&&ch==')') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        //遍历完了，但是栈不为空
        if(!stack.isEmpty())
            return false;
        return true;
    }
}